package cn.tedu.luban.demand.main.converter;

import cn.tedu.luban.demand.protocol.VO.RequestOrderListItemVO;
import cn.tedu.luban.demand.protocol.bo.RequestOrderBO;
import cn.tedu.luban.demand.protocol.dos.RequestOrderDO;
import cn.tedu.luban.demand.protocol.param.RequestOrderParam;
import cn.tedu.luban.demand.protocol.query.RequestOrderDBPagerQuery;
import cn.tedu.luban.demand.protocol.query.RequestOrderQuery;
import cn.tedu.luban.passport.protocol.jwt.UserPrinciple;
import cn.tedu.luban.passport.sdk.security.LubanSecurityContext;
import com.tedu.inn.protocol.ListRecordTotalBO;
import com.tedu.inn.protocol.pager.PagerResult;
import com.tedu.inn.protocol.pager.SimplePagerQuery;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

@Component
public class DemandConverter {

    public RequestOrderListItemVO assembleBO2VO(RequestOrderBO bo) {
        RequestOrderListItemVO requestOrder = new RequestOrderListItemVO();
        BeanUtils.copyProperties(bo, requestOrder);
        requestOrder.setRequestOrderNo(bo.getRequestOrderNo());
        requestOrder.setCreateTime(bo.getGmtCreate());
        requestOrder.setCategoryId(bo.getOrderCategoryId());
        return requestOrder;
    }





    public List<RequestOrderListItemVO> boListAssembleVOList(List<RequestOrderBO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        List<RequestOrderListItemVO> requestOrderVOList = new ArrayList<>(list.size());
        for (RequestOrderBO requestOrderBO : list) {
            RequestOrderListItemVO requestOrderVO = this.assembleBO2VO(requestOrderBO);
            requestOrderVOList.add(requestOrderVO);
        }
        return requestOrderVOList;
    }

    public PagerResult<RequestOrderListItemVO> assemblePagerResult(
        ListRecordTotalBO<RequestOrderBO> accessProviderListTotalRecord,
        SimplePagerQuery requestOrderQuery) {
        List<RequestOrderListItemVO> requestOrderVOList = this.boListAssembleVOList(accessProviderListTotalRecord.getList());
        PagerResult<RequestOrderListItemVO> pagerResult = new PagerResult<>(requestOrderQuery);
        pagerResult.setObjects(requestOrderVOList);
        pagerResult.setTotal(accessProviderListTotalRecord.getTotal());
        return pagerResult;
    }
    public RequestOrderDBPagerQuery toDbPagerQuery(RequestOrderQuery requestOrderQuery){
        if (requestOrderQuery == null) {
            return new RequestOrderDBPagerQuery();
        }
        RequestOrderDBPagerQuery requestOrderDBPagerQuery = new RequestOrderDBPagerQuery();
        BeanUtils.copyProperties(requestOrderQuery, requestOrderDBPagerQuery);
        return requestOrderDBPagerQuery;
    }

    public RequestOrderDO param2po(RequestOrderParam param) {
        RequestOrderDO requestOrder = new RequestOrderDO();
        BeanUtils.copyProperties(param, requestOrder);
        requestOrder.setRequestOrderNo(param.getRequestOrderNo());
        UserPrinciple loginUser = LubanSecurityContext.getLoginToken();
        requestOrder.setGmtCreate(System.currentTimeMillis());
        requestOrder.setGmtModified(requestOrder.getGmtCreate());
        requestOrder.setCreateUserId(loginUser!=null?loginUser.getId():99L);
        requestOrder.setModifiedUserId(loginUser!=null?loginUser.getId():99L);
        requestOrder.setCreateUserName(loginUser!=null? loginUser.getUsername(): "root");
        requestOrder.setModifiedUserName(loginUser!=null? loginUser.getUsername(): "root");

        return requestOrder;
    }

    public RequestOrderBO po2bo(RequestOrderDO requestOrder) {
        RequestOrderBO requestOrderBO = new RequestOrderBO();
        if (requestOrder == null ){
            return requestOrderBO;
        }
        BeanUtils.copyProperties(requestOrder, requestOrderBO);
        return requestOrderBO;
    }

    public List<RequestOrderBO> poList2BoList(List<RequestOrderDO> list) {
        List<RequestOrderBO> requestOrderBos = new ArrayList<>(list.size());
        for (RequestOrderDO requestOrder : list) {
            requestOrderBos.add(this.po2bo(requestOrder));
        }
        return requestOrderBos;
    }

}
